<?php

include_once 'Classes/PHPCodeProvider.php';

/*
 * Create an instance of the CodeGeneratorOptions class
 * using the default options for code generation.
 */

$options = new CodeGeneratorOptions();

/*
 * Create an instance of the IndentedTextWriter.
 * After the generateCodeFromStatement call
 * $writer will hold the generated code.
 */

$writer = new IndentedTextWriter();

/*
 * Create an instance of the PHPCodeProvider class
 */

$provider = new PHPCodeProvider();

/*
 * Expressions and statements to be used to create for statements
 */

$makeFooRef = new VariableReferenceExpression('makeFoo');

$namespace1 = new CodeNamespace('');

$makeFoo = new AssignStatement($makeFooRef, new PrimitiveExpression(true));

$namespace1->Statements->add($makeFoo);

$barInvoke = new ExpressionStatement(
    new FunctionInvokeExpression(
        new FunctionReferenceExpression('bar')));

$namespace1->Statements->add($barInvoke);

$ifStatement1 = new IfElseStatement($makeFooRef);

$function1 = new CodeFunction('foo');
$function1->Statements->add(
    new SnippetStatement(
        '        echo "I don\'t exist until program execution reaches me.\n"'));

$ifStatement1->TrueStatements->add($function1);

$namespace1->Statements->add($ifStatement1);

$ifStatement2 = new IfElseStatement($makeFooRef);
$ifStatement2->TrueStatements->add(
    new ExpressionStatement(
        new FunctionInvokeExpression(
            new FunctionReferenceExpression('foo'))));

$namespace1->Statements->add($ifStatement2);

$function2 = new CodeFunction('bar');
$function2->Statements->add(
    new SnippetStatement(
        '    echo "I exist immediately upon program start.\n"'));

$namespace1->Statements->add($function2);

/*
 * Generate the code and output the result.
 */

$provider->generateCodeFromNamespace($namespace1, $writer, $options);

echo 
    '<pre>' . str_replace(
        '<br />', '', highlight_string($writer->get_String(), true)) . 
    '<pre/>';


/*
 * The output should be:
 * 
 * 
 * 
 */
?>
